home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Metrowerks Versions / C06 QuickDraw GX / P06 QD GX Mapping / QDGXMapping.c next >
Encoding:
C/C++ Source or Header  |  1995-08-30  |  4.5 KB  |  198 lines  |  [TEXT/MMCC]

  1. //____________________________________________________________
  2. //    QDGXMapping.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #ifndef powerc
  13.    #pragma pointers_in_D0        
  14. #endif
  15.  
  16. #include "PrintingManager.h"      // defines the Gestalt selector code gestaltGXPrintingMgrVersion
  17. #include "graphics macintosh.h"   // defines the Gestalt selector code gestaltGraphicsVersion
  18. #include "graphics toolbox.h"     // prototype for the GX function GXNewWindowViewPort()
  19. #include "graphics libraries.h"   // prototypes for several GX functions
  20.  
  21. #ifndef powerc
  22.    #pragma pointers_in_A0
  23. #endif
  24.  
  25.  
  26. //____________________________________________________________
  27.  
  28. Boolean  IsQuickDrawGXAvailable( void );
  29. void     InitializeToolbox( void );
  30. void     InitializeQuickDrawGX( void );
  31. void     OpenDisplayWindow( void );
  32. void     CleanUpQuickDrawGXandQuit( void );
  33. void     CreateGXRectangleShape( void );
  34. void     ScaleGXRectangleShape( void );
  35.  
  36.  
  37. //____________________________________________________________
  38.  
  39. #define     kGXClientHeapSizeBytes    150 * 1024
  40.  
  41.  
  42. //____________________________________________________________
  43.  
  44. gxGraphicsClient  gGXClient;
  45. Boolean           gQuickDrawGXPresent;
  46. WindowPtr         gDisplayWindow = nil;
  47. gxViewPort        gWindowViewPort;
  48. Boolean           gDone = false;
  49. gxShape           gRectShape;
  50.  
  51.  
  52. //____________________________________________________________
  53.  
  54. void  main( void )
  55. {
  56.    InitializeToolbox();
  57.  
  58.    gQuickDrawGXPresent = IsQuickDrawGXAvailable();
  59.    if ( gQuickDrawGXPresent == true )
  60.       InitializeQuickDrawGX();
  61.    else
  62.       ExitToShell();
  63.  
  64.    OpenDisplayWindow();
  65.  
  66.    while ( gDone == false )
  67.    {
  68.       if ( Button() )
  69.          CleanUpQuickDrawGXandQuit();
  70.    }      
  71. }
  72.  
  73.  
  74. //____________________________________________________________
  75.  
  76. Boolean  IsQuickDrawGXAvailable( void )
  77. {
  78.    OSErr  theError;
  79.    long   theResult;
  80.    
  81.    theError = Gestalt( gestaltGraphicsVersion, &theResult );
  82.    if ( theError != noErr )
  83.       return ( false );
  84.  
  85.    theError = Gestalt( gestaltGXPrintingMgrVersion, &theResult );
  86.    if ( theError != noErr )
  87.       return ( false );
  88.       
  89.    return ( true );
  90. }
  91.  
  92.  
  93. //____________________________________________________________
  94.  
  95. void  InitializeQuickDrawGX( void )
  96. {
  97.    gxGraphicsError  theGXgraphicsError;
  98.    OSErr            theGXprintError;      
  99.    
  100.    gGXClient = GXNewGraphicsClient( nil, kGXClientHeapSizeBytes, 0L );
  101.  
  102.    GXEnterGraphics();
  103.    theGXgraphicsError = GXGetGraphicsError( nil );
  104.    if ( theGXgraphicsError == out_of_memory )
  105.       ExitToShell();
  106.  
  107.    theGXprintError = GXInitPrinting();
  108.    if ( theGXprintError != noErr )
  109.       ExitToShell();
  110. }
  111.  
  112.  
  113. //____________________________________________________________
  114.  
  115. void  OpenDisplayWindow( void )
  116. {
  117.    gDisplayWindow = GetNewCWindow( 128, nil, (WindowPtr)-1L ); 
  118.    ShowWindow( gDisplayWindow );
  119.    SetPort( gDisplayWindow );
  120.    
  121.    gWindowViewPort = GXNewWindowViewPort( gDisplayWindow );
  122.  
  123.    CreateGXRectangleShape();
  124.  
  125.    GXSetShapeViewPorts( gRectShape, 1, &gWindowViewPort );
  126.  
  127.    GXDrawShape( gRectShape );
  128.  
  129.    ScaleGXRectangleShape();
  130.  
  131.    GXDrawShape( gRectShape );
  132.  
  133. }
  134.  
  135.  
  136. //____________________________________________________________
  137.  
  138. void  CreateGXRectangleShape( void )
  139. {
  140.    gxRectangle  theRectGeometry = { ff(200), ff(40), ff(320), ff(120) };                           
  141.  
  142.    gRectShape = GXNewShape( gxRectangleType );
  143.    GXSetRectangle( gRectShape, &theRectGeometry );
  144. }
  145.  
  146.  
  147. //____________________________________________________________
  148.  
  149. void  ScaleGXRectangleShape( void )
  150. {
  151.    Fixed  theHorizScale;
  152.    Fixed  theVertScale;
  153.    Fixed  theXOffset = ff(0);
  154.    Fixed  theYOffset = ff(0);
  155.  
  156.    theHorizScale = FixedDivide( ff(1), ff(2) );
  157.    theVertScale  = FixedDivide( ff(1), ff(2) );
  158.  
  159.    GXScaleShape( gRectShape, theHorizScale, theVertScale, theXOffset, theYOffset );
  160. }
  161.  
  162.  
  163. //____________________________________________________________
  164.  
  165. void  CleanUpQuickDrawGXandQuit( void )
  166. {
  167.    OSErr  theGXprintError;      
  168.  
  169.    if ( gDisplayWindow != nil )
  170.    {
  171.       GXDisposeViewPort( gWindowViewPort );
  172.       DisposeWindow( gDisplayWindow );  
  173.    }
  174.    
  175.    theGXprintError = GXExitPrinting();
  176.  
  177.    GXExitGraphics();
  178.    
  179.    GXDisposeGraphicsClient( gGXClient ); 
  180.  
  181.    gDone = true;
  182. }
  183.  
  184.  
  185. //____________________________________________________________
  186.  
  187. void  InitializeToolbox( void )
  188. {
  189.    InitGraf( &qd.thePort );
  190.    InitFonts();
  191.    InitWindows();
  192.    InitMenus();
  193.    TEInit();
  194.    InitDialogs( 0L );
  195.    FlushEvents( everyEvent, 0 );
  196.    InitCursor();
  197. }
  198.